package com.coy.web.common.dto;

import com.alibaba.fastjson.annotation.JSONField;
import com.coy.web.common.exception.BizResultCode;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 数据响应DTO
 *
 * @author chenck
 * @date 2019/6/28 16:33
 */
@Data
public class DataResult<T> {
    /**
     * 返回码
     */
    private String retcode = BizResultCode.SUCC.getCode();

    /**
     * 描述信息
     */
    private String retmsg = BizResultCode.SUCC.getMsg();

    /**
     * 详细描述信息
     */
    private String detailMsg;

    /**
     * 返回业务数据
     */
    private T data;


    public DataResult() {
    }

    public DataResult(String retmsg) {
        this.retmsg = retmsg;
    }

    public DataResult(String retcode, String retmsg) {
        this.retcode = retcode;
        this.retmsg = retmsg;
    }

    public DataResult(T data, String retcode, String retmsg) {
        this.data = data;
        this.retcode = retcode;
        this.retmsg = retmsg;
    }

    public DataResult(T data, String retcode, String retmsg, String detailMsg) {
        this.data = data;
        this.retcode = retcode;
        this.retmsg = retmsg;
        this.detailMsg = detailMsg;
    }

    public DataResult<T> setData(T data) {
        this.data = data;
        return this;
    }

    /**
     * 校验是否成功
     */
    @JSONField(serialize = false)
    @JsonIgnore
    @ApiModelProperty(hidden = true)
    public boolean isSucc() {
        return BizResultCode.SUCC.getCode().equals(retcode);
    }

    /**
     * 设置结果
     */
    public static <T> DataResult<T> result(T data, String code, String msg) {
        return new DataResult(data, code, msg);
    }

    public static <T> DataResult<T> result(T data, String code, String msg, String detailMsg) {
        return new DataResult(data, code, msg, detailMsg);
    }

    /**
     * 设置为成功
     */
    public static DataResult succ() {
        return DataResult.result(null, BizResultCode.SUCC.getCode(), BizResultCode.SUCC.getMsg());
    }

    public static DataResult succ(String msg) {
        return DataResult.result(null, BizResultCode.SUCC.getCode(), msg);
    }

    public static <T> DataResult<T> succ(T data) {
        return DataResult.result(data, BizResultCode.SUCC.getCode(), BizResultCode.SUCC.getMsg());
    }

    public static <T> DataResult<T> succ(String msg, T data) {
        return DataResult.result(data, BizResultCode.SUCC.getCode(), msg);
    }

    public static <T> DataResult<T> succ(T data, String code, String msg) {
        return DataResult.result(data, code, msg);
    }

    /**
     * 设置为失败
     */
    public static DataResult error() {
        return DataResult.result(null, BizResultCode.ERR_SYSTEM.getCode(), BizResultCode.ERR_SYSTEM.getMsg());
    }

    public static DataResult error(String msg) {
        return DataResult.result(null, BizResultCode.ERR_SYSTEM.getCode(), msg);
    }

    public static DataResult error(String code, String msg) {
        return DataResult.result(null, code, msg);
    }

    public static DataResult error(String code, String msg, String detailMsg) {
        return DataResult.result(null, code, msg, detailMsg);
    }

    public static <T> DataResult<T> error(T data) {
        return DataResult.result(data, BizResultCode.ERR_SYSTEM.getCode(), BizResultCode.ERR_SYSTEM.getMsg());
    }

    public static <T> DataResult<T> error(T data, String code, String msg) {
        return DataResult.result(data, code, msg);
    }
}
